from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

from myapp.models import User,Goods,Category
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

#导入序列化对象
from myapp.myser import UserSer,GoodsSer,CategorySer

#定义地址和端口
host = '127.0.0.1'
port = 6379
#建立redis链接
r = redis.Redis(host=host,port=port)

'''
# #商品分类增删改查接口
# class Category_(APIView):
#     #查询接口
#     def get(self,request):
# 
#         category = Category.objects.all()
#         # print(category)
#         category_ser = CategorySer(category,many=True)
# 
#         return Response(category_ser.data)
# 
# 
#     #添加接口
#     def post(self,request):
#         cname = request.data.get('cname')
#         print(cname)
#         #查询操作
#         category = Category.objects.filter(cname=cname).first()
# 
#         #排重
#         if category:
#             res = {}
#             res['code'] = 405
#             res['message'] = "分类已存在"
# 
#             return Response(res)
#         else:
#             category = Category(cname=cname)
#             category.save()
# 
#             res = {}
#             res['code'] = 200
#             res['message'] = "分类添加成功"
# 
#             return Response(res)
# 
#     def delete(self,request):
#         id = request.data.get('id')
#         print(id)
#         try:
#             Category.objects.get(id=id).delete()
#         except Exception as e:
#             print(e)
# 
#             return Response('删除失败')
#         return Response({'message': '删除分类成功'})
# 
#     def options(self,request):
#         id = request.data.get('id')
#         print(id)
# 
#         cname = request.data.get('cname')
#         print(cname)
# 
#         category = Category.objects.filter(id=id).first()
# 
#         if category:
#             category.cname = cname
#             category.save()
# 
#             return Response({'message':'修改成功'})
#         else:
#             return Response({'message':'修改失败'})
# 
# 
# 
# #商品删改查接口
# class Goods_(APIView):
#     def get(self,request):
#         goods = Goods.objects.all()
# 
#         goods_ser = GoodsSer(goods,many=True)
#         return Response(goods_ser.data)
# 
#     def post(self,request):
#         gid = request.GET.get('gid')
# 
#         name = request.GET.get('name')
#         desc = request.GET.get('desc')
#         img = request.GET.get('img')
#         param = request.GET.get('param')
#         c_id = request.GET.get('c_id')
#         video = request.GET.get('video')
#         flow = request.GET.get('flow')
#         price = request.GET.get('price')
# 
# 
#         goods = Goods.objects.filter(id=gid).first()
#         goods.name = name
#         goods.desc = desc
#         goods.img = img
#         goods.param = param
#         goods.c_id = c_id
#         goods.video = video
#         goods.flow = flow
#         goods.price = price
#         goods.save()
# 
#         return Response({'message':'商品修改成功'})
# 
#     def delete(self,request):
#         gid = request.GET.get('gid')
#         try:
#             Goods.objects.get(id=gid).delete()
#         except Exception as e:
#             print(e)
# 
#             return Response('删除失败')
#         return Response({'message':'删除商品成功'})

#商品分类接口
'''

class CategotyInfo(APIView):
    def get(self,request):
        #商品查询
        category = Category.objects.filter()
        #序列化
        cate_ser = CategorySer(category,many=True)
        #返回
        return Response(cate_ser.data)

#商品入库接口
class AddGoods(APIView):
    def get(self, request):
        #获取uid
        uid = request.GET.get('uid')
        #查看内容和数据类型
        # print(uid)
        # print(type(uid))
        #查询users表
        users = User.objects.filter(id=uid).first()
        #查看users.type数据内容与数据类型
        # print(users.type)
        # print(type(users.type))
        #判断两个属性是否相等
        if int(uid) == users.type:
            #若相等则获取前端的其他数据
            name = request.GET.get('name')
            desc = request.GET.get('desc')
            img = request.GET.get('img')
            video = request.GET.get('video')
            param = request.GET.get('param')  # 入库时存入json
            cid = request.GET.get('cid')  # 入库时直接入整型
            price = request.GET.get('price')
            #print进行查看
            print(name, " ", desc, " ", img, " ", video, " ", param, " ", cid, " ", price)
            # 排重
            goods = Goods.objects.filter(name=name).first()
            #如果goods内容存在
            if goods:
                res = {}
                res['code'] = 405
                res['message'] = '商品已经存在'
                return Response(res)
            #如果goods内容不存在
            else:
                #进行存入数据库操作
                goods = Goods(name=name, desc=desc, img=img, video=video, param=param, cid=cid, price=price)
                #保存
                goods.save()

                res = {}
                res['code'] = 200
                res['message'] = '商品添加成功'

                return Response(res)
        else:
            return Response("您没有管理员权限,无法添加商品信息")

#商品列表页
class GoodsList(APIView):
    def get(self,request):
        #获取用户id
        uid = request.GET.get('uid')
        #读取数据库
        users = User.objects.filter(id=int(uid)).first()
        #进行序列化操作
        user_ser = UserSer(users)

        #进行返回
        return Response(user_ser.data)